 

function maintenanceCtrl($scope, $http, ngDialog, paFetchService) {
  'ngInject';

  this.maintenanceItemList = [];
  // 查询维保信息
  this.queryData = {};
  this.paginationData = {
    pageNum: 1,
    pageSize: 10,
    total: '',
  };
  //更换页码时
  $scope.pageChange = () => {
    $http.post(`${SERVER_CONTEXT}/api/devicemaintainrecord/selectMaintainRecordList`,
        angular.extend(this.queryData, this.paginationData)
        )
        .then((resp) => {
          this.maintenanceItemList = resp.data.data;
          this.paginationData.total = resp.data.total;
        });
  };
  // 查询
  this.MaintenanceInformation = () => {
    $http.post(`${SERVER_CONTEXT}/api/devicemaintainrecord/selectMaintainRecordList`,
        angular.extend(this.queryData, this.paginationData)
        ).
        then((resp) => {
          this.maintenanceItemList = resp.data.data;
          this.paginationData.total = resp.data.total;
        });
  };

  // 根据查询条件查询数据
  this.search = (param) =>{
    if (!param.startTime.$invalid && param.endTime.$invalid) {
      layer.alert('请填写维保结束时间');
      return false;
    } else if (param.startTime.$invalid && !param.endTime.$invalid) {
      layer.alert('请填写维保开始时间');
      return false;
    } else if (param.startTime.$valid && param.endTime.$valid) {
      if (param.startTime.$viewValue < param.endTime.$viewValue) {
        this.MaintenanceInformation();
        return false;
      } else {
        layer.alert('开始时间应小于结束时间');
      }
    } else {
      this.MaintenanceInformation();
    }   
  };
  // 重置查询
  this.reset = () =>{
    this.queryData = {};
  }

  //  初始页面
  this.initPage = () => {
    // 获得标段list
    paFetchService
      .getSectionList()
      .then((resp) => {
        this.sectionList = resp.data;
      });
    // this.MaintenanceInformation();
  }

  // 新增维保信息
  this.newInformation = () => {
    ngDialog.open({
      template: 'maintenanceInfo',
      className: 'ngdialog-theme-default pa-special-check-item-list-theme',
      controllerAs: 'addMaintenance',
      ariaAuto: false,
      scope: $scope,
      controller: function($http, $scope) {
        'ngInject';
        this.queryData = {};
        this.submit = (param) => {
          // 修改表单是否提交过状态
          param.$submitted = true;

          // 表单校验
          if (param.$invalid) {
            return false;
          }
          $http.post(`${SERVER_CONTEXT}/api/devicemaintainrecord/insertMaintainRecord`,
            this.queryData)
            .then((resp) => {
              if (resp.data.errorCode == '0') {
                layer.msg(resp.data.message);
                $scope.maintenance.MaintenanceInformation();
              } else {
                layer.alert(resp.data.message);
              }
              $scope.closeThisDialog();
            });
        }
      },
    }); 
  }

  // 删除信息
  this.deleteInformation = (id) => {
    let _this = this;
    $scope.modalTitle = '删除信息';
    $scope.modalContent = '确认要删除此信息吗?';
    ngDialog.openConfirm({
      template: 'paConfirm',
      className: 'ngdialog-theme-default pa-confirm-theme',
      scope: $scope,
    }).
    then((value) => {
      let reqData = [];
      reqData.push(id);
      $http({
        method: 'DELETE',
        url: `${SERVER_CONTEXT}/api/devicemaintainrecord/deleteMaintainRecord/${id}`
      }).
      then((data) => {
        this.MaintenanceInformation();
      });
    }, (reason) => {});
  };

  // 编辑维保信息
  this.editInfo = [];
  this.editInformation = (id) => {
    $scope.id = id;
    ngDialog.open({
      template: 'editmaintenanceInfo',
      className: 'ngdialog-theme-default pa-special-check-item-list-theme',
      scope: $scope,
      controller: function($scope) {
        'ngInject';
        let str = `${SERVER_CONTEXT}/api/devicemaintainrecord/queryMaintainRecordInfo/${id}`;
        $http.get(str).
        then((resp) => {
          $scope.editInfo = resp.data;
        });
        $scope.submit = (param) => {
          // 修改表单是否提交过状态
          param.$submitted = true;

          // 表单校验
          if (param.$invalid) {
            return false;
          }
          $http({
            method: 'PATCH',
            url: `${SERVER_CONTEXT}/api/devicemaintainrecord/updateMaintainRecord`,
            data: $scope.editInfo,
          }).then((resp) => {
            if (resp.data.success) {
              $scope.maintenance.MaintenanceInformation();
               layer.msg(resp.data.message);
              $scope.closeThisDialog();
            } else {
              layer.alert(resp.data.message);
              $scope.closeThisDialog();
            }
          });
        }
      }
    });
  }

  // 升序、降序
  this.up = (param,index) => {     
    this.queryData.columeName = param;   
    this.queryData.order = index;
    this.MaintenanceInformation();
  }

  // 初始化页面
  this.initPage();
}

export default maintenanceCtrl;